<template>
	<view>
		<baseHead title="企业福利" bg="#ffffff" color="#000000" text_right="保存" :right="true" @clickRight="clickRight">
		</baseHead>

		<view class="center">
			<view v-for="(list,index) in lists" :key="index">
				<view class="screen-title">{{list.title}}</view>
				<view class="fl-w  tabs">
					<view class="li" v-for="(item,i) in list.data" :key="i" :class="{'activation':item.type}"
						@click="clickItem(item,list,i)">{{item.FullName}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import baseHead from '@/pages/component/baseHead.vue'
	export default {
		data() {
			return {
				load: true, //是否加载完成
				lists: [{
					activation: 0,
					title: '加班情况',
					data: []
				}, {
					activation: 0,
					title: "保险",
					data: []
				}, {
					activation: 0,
					title: "福利待遇",
					data: []
				}, {
					activation: 0,
					title: "生活补贴",
					data: []
				}],

				model: {
					OverTime: "",
					Insurance: "",
					Benefits: "",
					Welfares: "",
				},

			}
		},
		components: {
			baseHead
		},
		computed: {},
		onLoad() {},
		created() {
			this.listdata();
			this.getIndustry();
			
		},
		onShow() {},
		// 初次渲染
		onReady() {},
		// 挂载完成调用
		mounted() {},
		// 计算方法
		methods: {
			listdata() {
				let qyid = uni.getStorageSync('dianjiqiye');
				this.$http.core.tenantgetall(qyid.Id).then(res => {
					if (res.Success) {
						console.log(res);
						this.model = res.Result;
					}
				})
			},
			clickRight() {
				console.log(this.model)

				this.lists[0].data.forEach(res => {
					if (res.type) {
						this.model.OverTime = res.EnCode
					}
				})
				let istsx1 = []
				this.lists[1].data.forEach(res => {
					if (res.type) {
						istsx1.push(res.FullName)
					}
				})
				this.model.Insurance = JSON.stringify(istsx1)
				let istsx2 = []

				this.lists[2].data.forEach(res => {
					if (res.type) {
						istsx2.push(res.FullName)
					}
				})
				this.model.Welfares = JSON.stringify(istsx2)

				let istsx3 = []

				this.lists[3].data.forEach(res => {
					if (res.type) {
						istsx3.push(res.FullName)
					}
				})
				this.model.Benefits = JSON.stringify(istsx3)

				//修改
				this.$http.core.tenantupdate(this.model).then(res => {
					console.log(res);
					if (res.Success) {
						this.$util.toast("修改成功")
						setTimeout(() => {
							uni.navigateBack();
						}, 1000)
					}
				})
			},
			getIndustry() {
				this.$http.core.dicAll().then(res => {
					if (res.Success) {
						console.log(res.Result)

						res.Result.forEach(resd => {
							if (resd.EnCode == "jbqk03") {
								this.lists[0].data = this.listfalse(resd.DictionaryList, 0);
							}
							if (resd.EnCode == "bx04") {
								this.lists[1].data = this.listfalse(resd.DictionaryList, 1);
							}
							if (resd.EnCode == "xzdy05") {
								this.lists[2].data = this.listfalse(resd.DictionaryList, 2);
							}
							if (resd.EnCode == "shbt06") {
								this.lists[3].data = this.listfalse(resd.DictionaryList, 3);
							}
						})

					}
				});
			},
			listfalse(arr, ind) {
				if (arr) {
					if (ind == 0) {
						let lis = this.model.OverTime;
						arr.forEach(res => {
							if (lis) {
								if (res.EnCode == lis) {
									res.type = true;
								} else {
									res.type = false;
								}
							}
						})
					}
					if (ind == 1) {

						if (this.model.Insurance) {
							let lis = JSON.parse(this.model.Insurance);
							arr.forEach(res => {
								res.type = false;
								if (lis) {
									lis.forEach(resitm => {
										if (res.FullName == resitm) {
											res.type = true;
										}
									})
								}
							})
						}

					}
					if (ind == 2) {
						
						if(this.model.Welfares){
							let lis = JSON.parse(this.model.Welfares);
							
							arr.forEach(res => {
								res.type = false;
								if (lis) {
									lis.forEach(resitm => {
										if (res.FullName == resitm) {
											res.type = true;
										}
									})
								}
							})
						}
						
					}
					if (ind == 3) {
						if(this.model.Benefits){
							
							let lis = JSON.parse(this.model.Benefits);
							arr.forEach(res => {
								res.type = false;
								if (lis) {
									lis.forEach(resitm => {
										if (res.FullName == resitm) {
											res.type = true;
										}
									})
								}
							})
						}
						
					}
					return arr;
				}

			},
			clickItem(item, list,i) {
				if (list.title == '加班情况') {
					list.data.forEach((res,key) => {
							this.$set(list.data,key,{FullName:res.FullName,type:false})	
					})
						this.$set(list.data,i,{FullName:item.FullName,type:true}) 
				} else {
					item.type = !item.type
				}
			},


		},
	}
</script>

<style lang="scss" scoped>
	.center {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;

		.screen-title {
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 800;
			color: #313131;
			margin: 30rpx 0;
		}

		.tabs {

			view {
				width: 216rpx;
				padding: 20rpx 30rpx;
				background: #ffffff;
				border-radius: 6rpx;
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
				margin-bottom: 20rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				text-align: center;
				box-sizing: border-box;
			}

			.li:nth-child(3n+2) {
				margin: 0 20rpx 20rpx;
			}

			.activation {
				color: #1684FC;
				background: #F1F8FF;
				border: 1rpx solid #1684FC;
				box-sizing: border-box;
			}

		}

	}
</style>